
<template>

  <template v-if="user">
    <van-cell title="昵称" is-link to="/user/edit" :value="user.userName"  @click="toEdit('userName', '昵称', user.userName)"/>
    <van-cell title="账号" :value="user.userAccount"/>
    <van-cell title="头像" is-link to="/user/edit">
      <img style="height: 48px" :src="user.userAvatar"/>
    </van-cell>
    <van-cell title="标签" is-link :value="user.tags" @click="toEdit('tags', '标签', user.tags)"/>
    <van-cell title="性别" is-link :value="user.gender" @click="toEdit('gender', '性别', user.gender)"/>
    <van-cell title="邮箱" is-link to="/user/edit" :value="user.email" @click="toEdit('email', '邮箱', user.email)"/>
    <van-cell title="编号" :value="user.id"/>
    <van-cell title="注册时间" :value="user.createTime"/>
  </template>
  <div style="margin: 13px;">
    <van-button type="primary" @click="toExit">登出</van-button>
  </div>

</template>

<script setup lang = 'ts'>
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../plugins/myAxios";
import {showFailToast, showSuccessToast, Toast} from "vant";
import {getCurrentUser} from "../services/user";

const user = ref();
const router = useRouter();

onMounted(async () => {
  const res = await getCurrentUser();
  if (res) {
    user.value = res;
    showSuccessToast('获取用户信息成功');
  } else {
    showFailToast('获取用户信息失败');
  }
})

const toExit = async () => {
  const res = await myAxios.post('/user/logout', {
  });
  if (res?.code === 0) {
    Toast.success('登出成功');

  } else {
    Toast.fail('登出失败' );
  }
}



const toEdit = (editKey: string,editName: string,currentValue: string) => {
  router.push({
    path:'/user/edit',
    query: {
      editKey,
      editName,
      currentValue,
    }
  })
}

</script>


<style scoped>

</style>